//
//  CLLHomeViewController.swift
//  CLWeiNo
//
//  Created by DDC on 2017/10/10.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
// cellIdentifier 
fileprivate let cellId = "cellId"
fileprivate let retweetCellId = "retweetCellId"

class CLLHomeViewController: CLLBaseViewController {
    
    /// 加载viewModel
    lazy var listViewModel = CLLStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
  
        
      
    }

    /// 重写父类加载数据的方法
    override func loadData() {
        
        listViewModel.loadStatus(pullUp: isPullUp) { (isSuccess) in
            
            // print("\(String(describing: self.listViewModel.statusList.last?.status   .text))")
            // 刷新表格
            self.tableView?.reloadData()
            self.refreshControl?.endRefreshing()
            // 恢复控件
            self.isPullUp = false
        }
        
    }
    // MARK:  - 导航栏点击方法
    @objc fileprivate func showFriends(){
        
        print(#function)
        
        let vc = CLLDemoViewController()
        
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
}
// MARK: - tableview属性及注册
extension CLLHomeViewController {
    /// 重写父类的设置界面方法
    override func setupTableView() {
        super.setupTableView()
        // 导航栏
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontsize: 16, action: #selector(showFriends), target: self)
        // 注册tableview
        // 原创微博注册o
        let nib = UINib(nibName: "CLLStatusNormalCell", bundle: nil)
        tableView?.register(nib, forCellReuseIdentifier: cellId)
        // 转发微博注册o 
        let nib2 = UINib(nibName: "CLLStatusRetweetCell", bundle: nil)
        tableView?.register(nib2, forCellReuseIdentifier: retweetCellId)
        tableView?.contentInset = UIEdgeInsetsMake(64, 0, 0, 0)
     
        // 设置预估行高
        tableView?.estimatedRowHeight = 300
        
        // 取消分割线
        tableView?.separatorStyle = .none
        
        setUpNavTitle()
    }
}

// MARK: - 设置导航栏标题
extension CLLHomeViewController {
    fileprivate func setUpNavTitle(){
        // 设置标题按钮
        let title = CLLNetWorkManager.shared.userAccount.screen_name
        let titleButton = CLLWBTtitleButton(title: title)
        
        // 添加点击事件
        titleButton.addTarget(self, action: #selector(titleButtonClick), for: .touchUpInside)
        // 将按钮设置为titleView
        navItem.titleView = titleButton
    }
    
    @objc func titleButtonClick (btn:UIButton){
        
        // 设置按钮的点击状态
        btn.isSelected = !btn.isSelected
    }
    
}
// MARK: - tableView
extension CLLHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 设置cell的模型
        let listModel = listViewModel.statusList[indexPath.row]
        
        // 根据是否是有被转发来显示不同的cell
        let displayCellId = listModel.status.retweeted_status != nil ? retweetCellId : cellId
        
        // 设置显示的cell
        let cell = tableView.dequeueReusableCell(withIdentifier: displayCellId, for: indexPath) as! CLLStatusCell
        
        // 设置数据
        cell.listModel = listModel
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let listModel = listViewModel.statusList[indexPath.row]
        
        return listModel.rowHeight
    }
}

